﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using System.Collections;

namespace School.Service.Uniform
{
    public static class SRV_Clothe_Issue
    {
        private static SchoolEntities db = new SchoolEntities();
        public static IEnumerable<object> VoucherSearch(long voucherNum,DateTime date)
        {

            try
            {
                var query = from payOrder in db.SRV_CLOTHES_PAYMENT_ORDER
                            where payOrder.Payment_Order_Number ==
                            (from voucher in db.ACC_Voucher where voucher.Number == voucherNum && voucher.Date == date &&
                             voucher.ORG_EductionalYear.IsCurrent == true select voucher).FirstOrDefault().Payment_Order_Number
                //            voucher.Date == date && 
                //            voucher.ORG_EductionalYear.IsCurrent == true
                            select new
                            {
                                Number = voucherNum,
                                Date = (from voucher in db.ACC_Voucher where voucher.Payment_Order_Number == payOrder.Payment_Order_Number select voucher).FirstOrDefault().Date,
                                SizeId = payOrder.SRV_Clothe_Sizes.Size_Id,
                                SizeName = payOrder.SRV_Clothe_Sizes.Name,
                                ClotheId = payOrder.SRV_Clothe_Sizes.SRV_Clothe.Id,
                                ClotheName = payOrder.SRV_Clothe_Sizes.SRV_Clothe.Name,
                                StudentId =payOrder.Student_Id,
                                StudentName = payOrder.TE_Student.Ar_FirstName + " " + payOrder.TE_Student.Ar_SecondName + " " + payOrder.TE_Student.Ar_ThirdNmae + " " + payOrder.TE_Student.Ar_FourthName,
                                VoucherValue = payOrder.QTY,
                                Paymnet_Order_Number = payOrder.Payment_Order_Number,
                                ExpenseId = (from voucher in db.ACC_Voucher where voucher.Payment_Order_Number == payOrder.Payment_Order_Number select voucher).FirstOrDefault().Expense_Id,
                                IsIssue = !(db.SRV_CLOTHES_TRANSACTION.Where(s => s.Payment_Order_Number == payOrder.Payment_Order_Number && s.Size_Id == payOrder.Clothes_Size_Id).FirstOrDefault().Payment_Order_Number.HasValue)
                            };
                return query.ToList();
            }
            catch 
            {
                return null;
            }
        
        }
        public static IEnumerable<object> VoucherSearch(long voucherNum)
        {
            try
            {
                var query = from payOrder in db.SRV_CLOTHES_PAYMENT_ORDER
                            where payOrder.Payment_Order_Number ==
                            (from voucher in db.ACC_Voucher
                             where voucher.Number == voucherNum &&
                                 voucher.ORG_EductionalYear.IsCurrent == true
                             select voucher).FirstOrDefault().Payment_Order_Number
                            //            voucher.Date == date && 
                            //            voucher.ORG_EductionalYear.IsCurrent == true
                            select new
                            {
                                Number = voucherNum,
                                Date = (from voucher in db.ACC_Voucher where voucher.Payment_Order_Number == payOrder.Payment_Order_Number select voucher).FirstOrDefault().Date,
                                SizeId = payOrder.SRV_Clothe_Sizes.Size_Id,
                                SizeName = payOrder.SRV_Clothe_Sizes.Name,
                                ClotheId = payOrder.SRV_Clothe_Sizes.SRV_Clothe.Id,
                                ClotheName = payOrder.SRV_Clothe_Sizes.SRV_Clothe.Name,
                                StudentId = payOrder.Student_Id,
                                StudentName = payOrder.TE_Student.Ar_FirstName + " " + payOrder.TE_Student.Ar_SecondName + " " + payOrder.TE_Student.Ar_ThirdNmae + " " + payOrder.TE_Student.Ar_FourthName,
                                VoucherValue = payOrder.QTY,
                                Paymnet_Order_Number = payOrder.Payment_Order_Number,
                                ExpenseId = (from voucher in db.ACC_Voucher where voucher.Payment_Order_Number == payOrder.Payment_Order_Number select voucher).FirstOrDefault().Expense_Id,
                                IsIssue = !(db.SRV_CLOTHES_TRANSACTION.Where(s => s.Payment_Order_Number == payOrder.Payment_Order_Number && s.Size_Id == payOrder.Clothes_Size_Id).FirstOrDefault().Payment_Order_Number.HasValue)
                            };
                return query.ToList();
            }
            catch
            {
                return null;
            }

        }
        public static IEnumerable<object> VoucherSearch(DateTime date)
        {

            try
            {
                var query = from payOrder in db.SRV_CLOTHES_PAYMENT_ORDER
                            where payOrder.Payment_Order_Number ==
                            (from voucher in db.ACC_Voucher
                             where voucher.Date == date &&
                                 voucher.ORG_EductionalYear.IsCurrent == true
                             select voucher).FirstOrDefault().Payment_Order_Number
                            //            voucher.Date == date && 
                            //            voucher.ORG_EductionalYear.IsCurrent == true
                            select new
                            {
                                Number = (from voucher in db.ACC_Voucher where voucher.Payment_Order_Number == payOrder.Payment_Order_Number select voucher).FirstOrDefault().Number,
                                Date = (from voucher in db.ACC_Voucher where voucher.Payment_Order_Number == payOrder.Payment_Order_Number select voucher).FirstOrDefault().Date,
                                SizeId = payOrder.SRV_Clothe_Sizes.Size_Id,
                                SizeName = payOrder.SRV_Clothe_Sizes.Name,
                                ClotheId = payOrder.SRV_Clothe_Sizes.SRV_Clothe.Id,
                                ClotheName = payOrder.SRV_Clothe_Sizes.SRV_Clothe.Name,
                                StudentId = payOrder.Student_Id,
                                StudentName = payOrder.TE_Student.Ar_FirstName + " " + payOrder.TE_Student.Ar_SecondName + " " + payOrder.TE_Student.Ar_ThirdNmae + " " + payOrder.TE_Student.Ar_FourthName,
                                VoucherValue = payOrder.QTY,
                                Paymnet_Order_Number = payOrder.Payment_Order_Number,
                                ExpenseId = (from voucher in db.ACC_Voucher where voucher.Payment_Order_Number == payOrder.Payment_Order_Number select voucher).FirstOrDefault().Expense_Id,
                                IsIssue = !(db.SRV_CLOTHES_TRANSACTION.Where(s => s.Payment_Order_Number == payOrder.Payment_Order_Number && s.Size_Id == payOrder.Clothes_Size_Id).FirstOrDefault().Payment_Order_Number.HasValue)
                            };
                return query.ToList();
            }
            catch
            {
                return null;
            }

        }
    }
}
